//
//  GoodsDetailsViewController.m
//  ShangPiBao
//
//  Created by chirui on 2017/10/30.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "GoodsDetailsViewController.h"

@interface GoodsDetailsViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    UITableView * _tableView;
    NSDictionary * goodsDic;
    
}
@end

@implementation GoodsDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    TitleView(@"产品详情");
    self.view.backgroundColor =[UIColor whiteColor];
//    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_nav_return"  target:self action:@selector(backClick)];
    [self creatTableView];
    [self getGoodsMessageForRequest];
}

-(void)backClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)creatTableView{
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT- 64) style:UITableViewStyleGrouped];
    _tableView.dataSource =self;
    _tableView.delegate =self;
    _tableView.backgroundColor =[UIColor whiteColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:_tableView];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 300;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UILabel * label =[[UILabel alloc]initWithFrame:CGRectMake(10, 15, WIDTH - 20, 20)];
    label.font = [UIFont systemFontOfSize:15];
    if (goodsDic) {
        
        label.text = goodsDic[@"pcontent"];
    }
    label.numberOfLines = 0;
    [label sizeToFit];
    return CGRectGetMaxY(label.frame)+20;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static  NSString * str = @"cellid";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:str];
    
    if (cell==nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }else {
        while ([cell.contentView.subviews lastObject]!=nil) {
            
            [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    
    UILabel * nameLabel =[[UILabel alloc]initWithFrame:CGRectMake(10, 15, WIDTH -20, 20)];
    nameLabel.font = [UIFont systemFontOfSize:15];
    if (goodsDic) {
        
        nameLabel.text = goodsDic[@"pcontent"];
    }
    nameLabel.numberOfLines = 0;
    [nameLabel sizeToFit];
    
    [cell.contentView addSubview:nameLabel];
    
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView * heardView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 300)];
    heardView.backgroundColor =[UIColor blackColor];
    
    UIView * lineView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 2)];
    lineView.backgroundColor = GoldTextColor;
    [heardView addSubview:lineView];
    
    UIImageView * imageView =[[UIImageView alloc]initWithFrame:CGRectMake((WIDTH-250)/2, 10, 250, 250)];
    imageView.backgroundColor =[UIColor whiteColor];
    imageView.layer.cornerRadius = 8.f;
    imageView.layer.masksToBounds = YES;
    imageView.image = ImageName(@"");
    [heardView addSubview:imageView];
    
    UILabel * nameLabel =[[UILabel alloc]initWithFrame:CGRectMake(0, 270, WIDTH, 20)];
    nameLabel.textColor = GoldTextColor;
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.font = [UIFont boldSystemFontOfSize:16];
    [heardView addSubview:nameLabel];
    
    if (goodsDic) {
        [imageView sd_setImageWithURL:ImageUrl(goodsDic[@"ppicture"]) placeholderImage:ImageName(@"img_home_newest")];
        [imageView tapAction:^{
            
            NSArray * imageArray = @[goodsDic[@"ppicture"]];
            
            XHPicView *picView = [[XHPicView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) withImgs:nil withImgUrl:imageArray];
            picView.eventBlock = ^(NSString *event){
                NSLog(@"触发事件%@",event);
            };
            UIWindow *window = [[UIApplication sharedApplication].delegate window];
            
            [window addSubview:picView];
        }];
        
        nameLabel.text = goodsDic[@"ptitle"];
    }

    return heardView;
}

-(void)getGoodsMessageForRequest{
    
    
    NSString * md5ver = [SHJudgeTools md532BitLower:[NSString
                                                     stringWithFormat:@"%@api.shangpibao.com",USER_DEFAULTGet(@"phone")]];
    
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@?uphone=%@&ver=%@&upid=%@&versions=new",address_url,get_goodsDetails_api,USER_DEFAULTGet(@"phone"),md5ver,self.goodsid];
    
    NSLog(@"parametersStr = %@",parametersStr);
    //    SHuiMBhubNoTimeLoding;
    [PostNetWorking netWorkingStr:parametersStr  Success:^(id responseObject) {
        SHuiMBHidleYes;
        if ([responseObject[@"err"] intValue]==0) {
            
            goodsDic = responseObject[@"data"][0];
            
        } else {
            
            SHuiMBhudTextTimeStr(EorrMessageStr);
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [_tableView reloadData];
        });
        
        
    } failed:^(NSError *error) {
        
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"请检查您的网络!");
    }];
    
}




@end
